package com.sl.au.controller;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Account;
import com.sl.au.entity.User;
import com.sl.au.entity.customerAllocation;
import com.sl.au.service.ExdeCustomerAllocationService;

@RestController
@RequestMapping(value="/ui/customerAllocationIndex")
public class customerAllocationController extends BaseController{
	
	@Autowired
	ExdeCustomerAllocationService exdeCustomerAllocationService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("customerAllocation/customerAllocationIndex");
		return modelAndView;
	}
	/***
	 * 获取审核客户所有人员数据List
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "Allgetpage", method = RequestMethod.GET)
	public Map<String, Object> getUserPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");
		List<customerAllocation> contracts = exdeCustomerAllocationService.getUser(start, length, key);
		long count = exdeCustomerAllocationService.getAllUserCount(key);
		map.put("data", contracts);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	/**
	 * 保存配置人员
	 * @param request
	 * @param customerallocation
	 * @return
	 */
	@RequestMapping(value = "saveCustomerAllocation", method = RequestMethod.POST)
	public Map<String, Object> saveCustomerAllocation(HttpServletRequest request, @RequestBody customerAllocation customerallocation) {
		Map<String, Object> map = new HashMap<String, Object>();
		Account a = this.getMyAccount();
		User user = a.getUser();
		String uuid = java.util.UUID.randomUUID().toString();
		if(!StringUtils.hasText(customerallocation.getId())) {
			customerallocation.setId(uuid);
		}
		exdeCustomerAllocationService.exdeCustomerAllocationRepository().save(customerallocation);
		String Id = customerallocation.getId();
		Date createTime = customerallocation.getCreateTime();
		map.put("code", 0);
		map.put("customerId", Id);
		map.put("createTime", createTime);
		return map;
	}
	/**
	 * 删除
	 * @param request
	 * @param customerallocation
	 * @return
	 */
	@RequestMapping(value = "delCustomerAllocation", method = RequestMethod.POST)
	public Map<String, Object> delCustomerAllocation(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Account a = this.getMyAccount();
		String id = request.getParameter("id");
		exdeCustomerAllocationService.exdeCustomerAllocationRepository().delete(id);
		map.put("code", 0);
		return map;
	}
	
}
